﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace HAZ.WinDBConnection
{
    public partial class MainForm : Form
    {
        ConnectionFactory.DBConnection dbConfig = ConnectionFactory.DBConnection.Default;

        ContextMenu cm = new ContextMenu();
        public MainForm()
        {
            InitializeComponent();

            MenuItem mi = new MenuItem("Fechar");
            mi.Click += new EventHandler(mi_Click);
            cm.MenuItems.Add(mi);

            new Forms.Logon().ShowDialog();
        }

        void mi_Click(object sender, EventArgs e)
        {
            try
            {
                ABAS.TabPages.RemoveAt(ABAS.SelectedIndex);
            }
            catch { }
        }

        private void tbtnConfigurar_Click(object sender, EventArgs e)
        {
            ToolsUtils.AddTab(ABAS, new Forms.Configurator(), "Configurador").ContextMenu = cm;
        }

        private void tbtnConsultar_Click(object sender, EventArgs e)
        {
            try
            {
                ToolsUtils.AddTab(ABAS, new Forms.DataResult(), "Consultas").ContextMenu = cm;
                //ConnectionFactory.IDBConnector db = ConnectionFactory.DBFactory.GetConnection(DBSuported.SqlClient);

                //OpenFileDialog open = new OpenFileDialog();
                
                //db.Select(new LoadFiles(@"c:\resetroot.sql").readFileWithTextMode());
            }
            catch { }
        }

        private void tbtnConsultaPreco_Click(object sender, EventArgs e)
        {
            if (Session.Serial != ConnectionFactory.DBConnection.Default.Licence)
            {
                MessageBox.Show("Acesse com permissão de configuração e verifique a licença de uso."
                          + "\r\nCaso necessário, consulte o Administrador ou o licenciador do produto.");
            }
            else if(DateTime.Today > Session.DataAtual){
                MessageBox.Show(String.Format("{0} Licença de uso do sistema expirou({1}), renove-a!"
                    , Session.DataAtual.ToString("dd/MM/yyyy")
                    , dbConfig.DataVencimento.ToString("dd/MM/yyyy")));
            }
            else
            {
                ToolsUtils.AddTab(ABAS, new Forms.ConPreco(), "Consulta Preço").ContextMenu = cm;
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.Text = dbConfig.Alias + " " + this.Text;

            tbtnConsultaPreco.Enabled = false;
            tbtnConfigurar.Enabled    = false;

            if ( Session.Password == dbConfig.PwdConfig
              || Session.Password == dbConfig.PwdInternal )
            {
                tbtnConsultaPreco.Enabled = true;
                tbtnConfigurar.Enabled    = true;

                tbtnConsultaPreco_Click(sender, e);
            }
            else if (Session.Password == dbConfig.PwdAcess)
            {
                tbtnConsultaPreco.Enabled = true;

                tbtnConsultaPreco_Click(sender, e);
            }
            else
            {
                MessageBox.Show("Acesso restrito.\r\nSenha informada sem permissão de uso do sistema.");
                this.Close();
                return;
            }
        }

        public void ReloadMainForm()
        {
            MainForm_Load(null, null);
        }
    }
}

namespace HAZ
{
    public partial class ToolsUtils
    {
        #region    Tratamento de ABAS
        public static TabPage AddTab(TabControl inControl, UserControl control, string title)
        {
            TabPage page = new TabPage();
            page.Text    = title;
            control.Dock = DockStyle.Fill;
            page.Controls.Add(control);

            return AddTab(inControl, page);
        }
        public static TabPage AddTab(TabControl inControl, TabPage page)
        {
            if (string.IsNullOrEmpty(page.Text))
                throw new Exception("Erro ao adicionar aba sem titulo.");

            foreach (TabPage p in inControl.TabPages)
                if (p.Text == page.Text)
                {
                    inControl.SelectedTab = p;
                    return p;
                }

            inControl.TabPages.Add(page);
            inControl.SelectedTab = page;
            
            return page;
        }

        public static void RemoveTab(Control ctr)
        {
            Control ctrIsTab = ctr.Parent;

            if (ctrIsTab == null) return;

            if (ctrIsTab.GetType() != typeof(TabPage) && ctrIsTab.Parent != null)
                RemoveTab(ctrIsTab.Parent);

            if (ctrIsTab.GetType() == typeof(TabPage) && ctrIsTab.Parent != null)
                RemoveTab((TabControl)ctrIsTab.Parent, (TabPage)ctrIsTab);
        }
        public static void RemoveTab(TabControl inControl, int Idx)     
        {
            if (inControl.TabPages.Count <= Idx)
                inControl.TabPages.RemoveAt(Idx);
        }
        public static void RemoveTab(TabControl inControl, string title)
        {
            TabPage page = null;

            foreach (TabPage p in inControl.TabPages)
                if (title == p.Text)
                    page = p;

            RemoveTab(inControl, page);
        }
        public static void RemoveTab(TabControl inControl, TabPage page)
        {
            if (inControl.TabPages.Contains(page) && page != null)
                inControl.TabPages.Remove(page);
        }
        #endregion Tratamento de ABAS


    }
}
